package org.example.service;

import org.example.entity.MyUser;
import org.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class SecurityUserDetailsService implements UserDetailsService {


    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {


        MyUser myUser = userMapper.getUserByName(username);

        if(myUser == null){
            throw new UsernameNotFoundException(username);
        }


        UserDetails userDetails = User.builder()
                .username(myUser.getName())
                .password("{noop}"+myUser.getPassword())
                .roles("USER")
                .build();

        return userDetails;
    }
}
